package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.mapper.UserMapper;
import com.situ.mall.pojo.User;
import com.situ.mall.pojo.query.UserQuery;
import com.situ.mall.service.IUserService;
import com.situ.mall.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    // A -> B  100
    @Override
    public LayUITableResult list(UserQuery userQuery) {
        //开启分页
        PageHelper.startPage(userQuery.getPage(), userQuery.getLimit());
        // 开启分页之后，查找所有后面会自动拼接上分页相关信息limit
        List<User> list = userMapper.list(userQuery);
        //查找一共有多少数量
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();

        return LayUITableResult.ok((int)totalCount, list);
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        userMapper.deleteAll(ids);
    }

    @Override
    public void add(User user) {
        userMapper.insertSelective(user);
    }

    @Override
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }
}
